package com.zhny.controller;

import com.github.pagehelper.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zhny.entity.PageResult;
import com.zhny.entity.QueryPageBean;
import com.zhny.entity.Result;
import com.zhny.pojo.Knowledge;
import com.zhny.service.KnowledgeService;
import com.zhny.utils.BaiDuScrapeUtils;
import com.zhny.utils.ImgSearchUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {

    @Reference
    //@Autowired
    private KnowledgeService knowledgeService;

    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable("page") int page, @PathVariable("size") int size ,@RequestParam(value = "queryString",required = false)/*,@PathVariable("queryString")*/ String queryString) {
        QueryPageBean queryPageBean = new QueryPageBean();
        queryPageBean.setCurrentPage(page);
        queryPageBean.setPageSize(size);
        queryPageBean.setQueryString(queryString);
        Page<Knowledge> knowledgePage = knowledgeService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(knowledgePage.getTotal(), knowledgePage.getResult());
        //queryPageBean.setQueryString(knowledgePage);
        return new Result(true, "查询成功", pageResult);
    }


    /***
     * 新增数据
     * @param knowledge
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Knowledge knowledge) {
        knowledgeService.add(knowledge);
        return new Result(true, "添加成功");
    }

    /**
     * 修改数据
     *
     * @param knowledge
     * @param id
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody Knowledge knowledge, @PathVariable("id") Integer id) {
        knowledge.setId(id);
        knowledgeService.update(knowledge);
        return new Result(true, "修改成功");
    }

    /***
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") List<Integer> id) {
        knowledgeService.delete(id);
        return new Result(true, "删除成功");
    }
}